#ifndef _DEBOUNCE_C_
#define _DEBOUNCE_C_

#include "debounce.h"

char countA, countB, filteredData;

void initDebounce( void )
{
   countA = 0;
   countB = 0;
   filteredData = 0;
}

char debounce( char newData )
{
	char delta, changes;

   delta = newData ^ filteredData;	// find all the changed bits

   countA ^= countB;						// increment the counters
   countB = ~countB;						//

   countA &= delta;						// reset the counters if no changes
   countB &= delta;						//   were detected

   changes = ~( ~delta | countA | countB );
   filteredData ^= changes;

	return filteredData;
}

#endif
